<?php

namespace common\models;

use common\logic\HouseLogic;
use Yii;

/**
 * This is the model class for table "{{%report}}".
 *
 * @property int $id
 * @property string $contacts 联系人
 * @property string $phone 联系电话
 * @property int $referee 推荐人
 * @property string $status 自看；推荐
 * @property int $uid 用户id
 * @property int $sign 签约 默认0，1：签约；2：否
 * @property int $return_time 返现时间
 * @property string $return_amount 返现金额
 * @property int $sign_time 签约时间
 * @property string $sign_amount 签约金额
 * @property string $sign_house 签约户型
 * @property int $house_id 房源名称
 * @property int $create_time 报备时间
 */
class ReportModel extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%report}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['referee', 'uid', 'sign','house_id','create_time'], 'integer'],
            [['return_time', 'sign_time'], 'safe'],
            [['return_amount', 'sign_amount'], 'number'],
            [['contacts','sign_house'], 'string', 'max' => 50],
            [['phone', 'status'], 'string', 'max' => 20],
            [['sign_house'],'requiredByASpecial']
        ];
    }

    public function requiredByASpecial($attribute, $params)
    {
        if (!empty($this->sign_house))
        {
            $house = HouseLogic::instance()->getHouseDetail($this->sign_house);
            if (empty($house))
                $this->addError($attribute, "户型不存在");
        }
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'contacts' => '联系人',
            'phone' => '联系电话',
            'referee' => '推荐人',
            'status' => '状态',
            'uid' => '用户id',
            'sign' => '是否签约',
            'return_time' => '返现时间',
            'return_amount' => '用户返现金额(万)',
            'sign_time' => '签约时间',
            'sign_amount' => '签约金额(万)',
            'sign_house' => '签约户型',
            'house_id'=>'房源名称',
            'create_time'=>'报备时间'
        ];
    }
    public function getUser()
    {
        return $this->hasOne(UserModel::className(), ['id' => 'referee']);
    }

    public function getHouse()
    {
        return $this->hasOne(HouseModel::className(), ['id' => 'house_id']);
    }
}
